package com.example.zhangch.myapplication;
        import android.app.Activity;
        import android.content.Context;
        import android.support.v7.app.AppCompatActivity;
        import android.os.Bundle;
        import android.support.v7.widget.LinearLayoutManager;
        import android.support.v7.widget.RecyclerView;
        import android.view.LayoutInflater;
        import android.view.View;
        import android.view.ViewGroup;
        import android.widget.AdapterView;
        import android.widget.ImageView;
        import android.widget.TextView;
        import java.util.ArrayList;
        import java.util.Arrays;
        import java.util.List;
        import android.view.View.OnClickListener;
        import android.view.View.OnLongClickListener;
        import android.widget.AdapterView.OnItemClickListener;
/**
 * Created by zhangch on 2017/7/12.
 */
public class GalleryAdapter extends
        RecyclerView.Adapter<GalleryAdapter.myViewHolder>  implements View.OnClickListener
{
    private LayoutInflater mInflater;
    private List<String> mDatas;
    private OnItemClickListener mOnItemClickListener=null;
    public GalleryAdapter(Context context, List<String> datats)
    {
        mInflater = LayoutInflater.from(context);
        mDatas = datats;
    }
    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            //注意这里使用getTag方法获取position
            mOnItemClickListener.onItemClick(v,(int)v.getTag());
        }
    }
    public void setOnItemClickListener(OnItemClickListener listener){
         this.mOnItemClickListener = listener;
    }
    public static interface OnItemClickListener {
        void onItemClick(View view , int position);
    }
    public String getItem(int position) {
        return mDatas == null ? null : mDatas.get(position);
    }

    @Override
    public int getItemCount()
    {
        return mDatas.size();
    }
    @Override
    public myViewHolder onCreateViewHolder(ViewGroup viewGroup, int i)
    {
        View view = mInflater.inflate(R.layout.activity_recycler_item,
                viewGroup, false);
        myViewHolder viewHolder = new myViewHolder(view);
        viewHolder.mTxt = (TextView) view
                .findViewById(R.id.id_index_gallery_item_text);
        view.setOnClickListener(this);
        return viewHolder;
    }
    @Override
    public void onBindViewHolder(final myViewHolder viewHolder, final int i)
    {
        viewHolder.mTxt.setText(mDatas.get(i));
        viewHolder.itemView.setTag(i);
    }
    public class myViewHolder extends RecyclerView.ViewHolder
    {
        public myViewHolder(View arg0)
        {
            super(arg0);
        }
        TextView mTxt;
    }
}
